package interfaz;

import java.io.*; 
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.payment.TransactionListener;
import javax.microedition.payment.TransactionListenerException;
import javax.microedition.payment.TransactionModule;
import javax.microedition.payment.TransactionModuleException;
import javax.microedition.payment.TransactionRecord;
import mundo.*;

public class RoboMindMidlet extends MIDlet implements CommandListener {
       
    public Display display;
    public RoboMindCanvas screen;
    public Screen gameScreen;
    public Screen scriptScreen;
    public Screen mapScreen;
    public Engine engine;
    public RobotAndMap juego;
    public Command exitCommand = new Command ("Exit", Command.EXIT, 1);  
    public Command playCommand = new Command ("Play", Command.SCREEN, 1); 
    
    public Command backCommand = new Command ("Back", Command.BACK, 1); 
    public Command optionsCommand = new Command ("Options", Command.SCREEN, 1);
    
    public Command scriptCommand = new Command ("Script", Command.SCREEN, 1);
    public Command mapCommand = new Command ("Map", Command.SCREEN, 1);
    
    public Command openCommand = new Command ("Open", Command.SCREEN, 1);
    public Command executeCommand = new Command ("Execute", Command.SCREEN, 1);
    public Command sendCommand = new Command ("Send", Command.SCREEN, 1);
    public Command commandCommand = new Command ("Commands", Command.SCREEN, 1);
  
    public Command okCommand = new Command ("Ok", Command.OK, 1);

    public Command codeCommand = new Command ("Code", Command.SCREEN, 1);
    public Command loadCommand = new Command ("Load", Command.SCREEN, 1);



    
   
    public TextBox scriptText;  
    List scriptMenu = null;
    public int lineaScript = 0;
    public int amount=0;
    public TextField scriptField = new TextField(null, "", 1, TextField.NUMERIC);
    private final ChoiceGroup scriptcg =
        new ChoiceGroup("Commands", ChoiceGroup.POPUP,
        		 new String[] { "right(1)", "left(1)", "forward(x)", "backward(x)" }, null);
    public Form scriptForm;
    
    public TextBox mapText; 
    List mapMenu = null;
    public int lineaMap = 0;
    public int bloqueMap = 0;
    public String mapString="";
    public TextField mapField = new TextField(null, "", 10, TextField.ANY);
//    private final ChoiceGroup mapcg =
//        new ChoiceGroup("Code", ChoiceGroup.POPUP,
//        		new String[] { "B", "L", "P", "W" }, null);
    public Form mapForm;

    public RoboMindMidlet() {
    	screen = new RoboMindCanvas(this);
    	engine = new Engine(screen);
        display = Display.getDisplay (this);
        screen.initialize ();
		screen.addCommand (exitCommand);
		screen.addCommand (playCommand);
        screen.setCommandListener (this);           
        display.setCurrent(screen);
    	
        scriptForm = new Form("Commands");
	    scriptForm.append(scriptcg);
	    scriptForm.append(scriptField);
	    scriptForm.addCommand(okCommand);
	    scriptForm.setCommandListener(this);	
	    
	    mapForm = new Form("Code");
	    mapForm.append(mapField);
	    mapForm.addCommand(okCommand);
	    mapForm.setCommandListener(this);
    }
    
    public void startApp() {
    	if (engine != null) {
            engine.setPaused(false);
        }
    }

    public void pauseApp() {
        if (engine != null) {
            engine.setPaused(true);
        }
    }
    
    public void destroyApp(boolean unconditional) {
        if (engine != null) {
            engine.stop();
        }
        Display.getDisplay(this).setCurrent(null);
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable s) {
    	
    	if (c == exitCommand) {
    		destroyApp (false);
    		notifyDestroyed ();
        }
    	else if (c == playCommand ) { 
    		engine.startGame();
    		screen.removeCommand (exitCommand);
    		screen.removeCommand (playCommand);
    		screen.addCommand (backCommand);
    		screen.addCommand (scriptCommand);
    		screen.addCommand (mapCommand);
    		display.setCurrent (screen);
    	}
        else if (c == scriptCommand) {
        	scriptText = new TextBox("Enter commands:", "", 100, TextField.UNEDITABLE);
    		scriptText.setCommandListener (this); 
	 		scriptText.addCommand(commandCommand);
	 		scriptText.addCommand(executeCommand); 
	 		scriptText.addCommand(openCommand);
	 		scriptText.addCommand(sendCommand); 
        	display.setCurrent (scriptText);
        }
        else if (c == commandCommand) {    		
     	    display.setCurrent (scriptForm);
        }  
        else if ((c == okCommand) && (s == scriptForm)) {
        	String cadena = scriptField.getString();
            switch(scriptcg.getSelectedIndex()) {
              case 0: 
            	  scriptText.insert("right(1)\n", lineaScript);
            	  lineaScript=lineaScript+10;
              break;
              case 1: 
            	  scriptText.insert("left(1)\n", lineaScript);
            	  lineaScript=lineaScript+10;
              break;
              case 2:
            	  scriptText.insert("forward("+cadena+")\n", lineaScript);            	  
            	  lineaScript=lineaScript+10;
              break;
              case 3: 
            	  scriptText.insert("backward("+cadena+")\n", lineaScript); 
            	  lineaScript=lineaScript+10;
              break;
            } 
        	display.setCurrent (scriptText);
       }
        else if (c == executeCommand) {    		
     	    juego = screen.getJuego();
     	    juego.expr(scriptText.getString());
     	    System.out.println("x "+juego.getX());
    	    System.out.println("y "+juego.getY());
     	    screen.setJuego(juego);
        	screen.repaint();
        	display.setCurrent (screen);
        } 
        else if (c == mapCommand) {
        	mapText = new TextBox("Enter map:", "", 100, TextField.UNEDITABLE);   		
    		mapText.setCommandListener (this); 
	 		mapText.addCommand(codeCommand);
	 		mapText.addCommand(loadCommand); 
	 		mapText.addCommand(openCommand);
	 		mapText.addCommand(sendCommand); 
        	display.setCurrent (mapText);
        }
        else if (c == codeCommand) {    		
     	    display.setCurrent (mapForm);
        } 

       else if ((c == okCommand) && (s == mapForm)) {
        	mapText.insert(mapField.getString()+"\n", lineaMap);
        	mapString=mapString+mapField.getString();
        	lineaMap=lineaMap+20;
        	bloqueMap = 0;
        	mapField.delete(0,mapField.size());
        	display.setCurrent (mapText);
       }
       else if (c == loadCommand) {
    	screen.loadMap(mapText.getString());
    	display.setCurrent (screen);
      }

    	
    	
    

    }
    

}
  


